﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;


namespace LoD.WPClientLibrary.Controls {
    public class ImageButton : GuiControl {
        public ImageButton(ContentManager content, SpriteBatch spriteBatch, SpriteFont font, string filename)
            : base(content, spriteBatch, font) {
            this.Text = string.Empty;
            Color = Color.White;
            this.TextColor = Color.White;
            this.ShadowColor = Color.Black;
            this.UseShadow = true;
            // ReSharper disable DoNotCallOverridableMethodsInConstructor
            Load(filename);
            // ReSharper restore DoNotCallOverridableMethodsInConstructor
        }

        public string Text { get; set; }
        public Color ShadowColor { get; set; }
        public Color TextColor { get; set; }
        public bool UseShadow { get; set; }
        public bool Tapped { get; set; }

        public override void Update(TouchLocation touch) {
            base.Update(touch);
            this.Tapped = false;
            if (touch.State != TouchLocationState.Pressed) return;
            Rectangle rect = Boundary();
            Vector2 pos = touch.Position;
            var point = new Point((int)pos.X, (int)pos.Y);
            if (rect.Contains(point)) this.Tapped = true;
        }

        public override void Draw() {
            base.Draw();
            Vector2 size = this.TextSize();
            var pos2 = new Vector2(Position.X + 2, Position.Y + 2);
            var pivot = new Vector2(size.X / 2, size.Y / 2);
            if (this.UseShadow) {
                SpriteBatch.DrawString(Font, this.Text, Position, this.ShadowColor, 0.0f, pivot, 1.0f,
                                       SpriteEffects.None, Zindex);
            }
            SpriteBatch.DrawString(Font, this.Text, pos2, this.TextColor, 0.0f, pivot, 1.0f, SpriteEffects.None, Zindex);
        }

        public Vector2 TextSize() { return Font.MeasureString(this.Text); }
    }
}